package atm05;

import atm04.FileUtil;
import atm04.Users;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class ATM05 extends JFrame {
    public static final String FILE_PATH  = "users.dat";
    private ArrayList users;
    private Image bgimg = new ImageIcon("src\\atm05\\Images\\background.jpg").getImage();
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);
    private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);
    private Users loginUser = new Users();
    public int index;



    public ATM05(){
        this.init();

        this.setContentPane(new JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(bgimg,0,0,this);
            }
        });
        this.setLayout(cardLayout);

        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(savePanel,"save");
        this.add(searchPanel,"search");
        this.add(getPanel,"get");
        this.add(transferPanel,"transfer");
        this.add(changePwdPanel,"change");


        this.setTitle("ATM");
        this.setSize(1024,768);
        //设置不能更改窗口
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }
    /**
     * 初始化集合
     */
    public void init(){
        users = (ArrayList) FileUtil.readObject(FILE_PATH);
        if(users == null){
            users = new ArrayList<>();
            users.add(new Users("aaaaaa","111111",1000));
            users.add(new Users("bbbbbb","222222",2000));
            users.add(new Users("cccccc","333333",3000));
            users.add(new Users("dddddd","444444",4000));
            users.add(new Users("eeeeee","555555",5000));
            FileUtil.writeObject(users,FILE_PATH);
        }
    }

    public void showPanel(String name) {
        cardLayout.show(this.getContentPane(),name);
    }

    public ArrayList<Users> getUsers() {
        return users;
    }

    public void setUsers(ArrayList users) {
        this.users = users;
    }

    public static void main(String[] args) {
        new ATM05();
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public void setLoginUser(Users loginUser) {
        this.loginUser = loginUser;
    }

    public Users getLoginUser() {
        return loginUser;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
